package com.itheima.service.impl.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.TokenHolder;
import com.itheima.dao.ShoppingCartDao;
import com.itheima.domain.ShoppingCart;
import com.itheima.exception.NoDataException;
import com.itheima.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
//todo:这里接口文档以及数据库设计的是只能存在订单或者套餐二者之一,但是实际上购物车下拉列表的时候是可以
// 菜品和套餐一起点的,所以 1.同时存在多个userid的表,前端将同一个userId整合到了一起显示 . 2.把number字段拆分成
//  套餐number 和  菜品number .需要后续前端页面的确认.这里按接口文档写

//艹,我TM是个傻逼
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartDao, ShoppingCart> implements ShoppingCartService {
    @Override
    public ShoppingCart add(ShoppingCart shoppingCart) {
        log.info("购物车数据:{}", shoppingCart);

        String currentId = TokenHolder.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, currentId);
        //判断新加入的是否是套餐
        if (shoppingCart.getDishId() == null) {
            //套餐
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        } else {
            //单品
            queryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        }
        ShoppingCart shoppingCartOne = this.getOne(queryWrapper);
        String createTime = LocalDateTime.now() + "";
        //判断是否是第一次新增
        if (shoppingCartOne == null) {
            shoppingCart.setCreateTime(createTime);
            shoppingCart.setUserId(currentId);
            this.save(shoppingCart);
            return shoppingCart;
        }
//        LambdaQueryWrapper<ShoppingCart> quertWrapper1 = new LambdaQueryWrapper<>();
//
//        //判断新加入的是否是套餐
//        if (shoppingCart.getDishId() == null) {
//            //套餐
//            quertWrapper1.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
//        } else {
//            //单品
//            quertWrapper1.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
//        }
        //肯定是存在的,直接在原来的number字段上+1
        ShoppingCart cart = this.getOne(queryWrapper);
        cart.setNumber(cart.getNumber() + 1);
        cart.setCreateTime(createTime);
        this.updateById(cart);

        return cart;
    }
//todo:需要在前台页面跑一下看看哪里能改; 存在一个即使数据不存在也会返回删除成功的问题
    @Override
    public boolean subone(Map map) {
        //获取前台传来的数据,判断是菜品还是套餐
        System.out.println("--------------------------------");
        String dishId = null;
        String setmealId = null;
        if (map.get("dishId") != null) {
            dishId = map.get("dishId").toString();
        } else if (map.get("setmeal") != null) {
            setmealId = map.get("setmeal").toString();
        }

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, TokenHolder.getCurrentId());
        List<ShoppingCart> carts = this.list(queryWrapper);

        //删除菜品或者套餐
        for (ShoppingCart cart : carts) {
            if (dishId != null && dishId.equals(cart.getDishId())) {
                queryWrapper.eq(ShoppingCart::getDishId, cart.getDishId());
            } else if (setmealId != null && setmealId.equals(cart.getSetmealId())) {
                queryWrapper.eq(ShoppingCart::getSetmealId, cart.getSetmealId());
            } else {
                log.info("参数有误");
                throw new NoDataException();
            }
            ShoppingCart cart1 = this.getOne(queryWrapper);
            if (cart1 == null) {
                log.info("该菜品或者套餐已经删除");
                return false;
            }
            Integer number = cart1.getNumber();
            if (number > 1) {
                number = number - 1;
                cart1.setNumber(number);
                this.updateById(cart1);
                return true;
            } else if (number == 1) {
                this.remove(queryWrapper);

            }
        }
        return true;
    }
}
